const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const schema = require('../db/schema');
class mongo {
    constructor(url, dbName) {
        this.url = url;
        this.dbName = dbName;
        this.client = '';
        this.connect().then().catch(e => {
            console.log(e);
        });
    }
    connect() {
        return new Promise((resolve, reject) => {
            if(this.client === '') {
                this.client = mongoose.connect(this.url, {useNewUrlParser: true, useUnifiedTopology: true, user: 'qxfront', pass: 'xDFaXEsV7CBgmo7Rs9mK'}, (err) => {
                    // console.log(err)
                    reject(err);
                });
                // this.client = mongoose.connect(this.url, {useNewUrlParser: true, useUnifiedTopology: true, }, (err) => {
                //     // console.log(err)
                //     reject(err);
                // });
                mongoose.connection.on('connected', () => {
                    resolve(this.client);
                });
                mongoose.connection.on('disconnected', (err) => {
                    console.log(err, 1111);
                    reject(err);
                });
            } else {
                resolve(this.client);
            }   
        });
    }   
    /**
     *  
     * @param {String} tableName 表名
     * @param {Array} insertData 插入数组
     * @param {Function} selfFunction 回调方法
     * @memberof mongo
     */
    insert(tableName, insertData, selfFunction) {
        this.connect().then(() => {
            schema[tableName].insertMany(insertData, (err, docs)=> {
                console.log(err);
                if(err) {
                    selfFunction(err);
                } else {
                    selfFunction(docs);
                }
            })
        });
    }
    /**
     *
     *
     * @param {*} tableName 表名
     * @param {*} whereStr 查询条件
     * @param {*} updateStr 需要更新数据
     * @param {*} selfFunction 回调方法
     * @memberof mongo
     */
    update(tableName, whereStr, updateStr, selfFunction) {
        this.connect().then(() => {
            schema[tableName].updateMany(whereStr,updateStr, (err, doc)=> {
                console.log(err);
                selfFunction(doc);
            });
        });
    }
    /**
     *
     *
     * @param {*} tableName 表名
     * @param {*} whereStr 查询条件
     * @param {*} selfFunction 回调方法
     * @memberof mongo
     */
    find(tableName, whereStr, selfFunction) {
        this.connect().then(() => {
            schema[tableName].find(whereStr, (err, doc)=> {
                console.log(err);
                selfFunction(doc);
            });
        });
    }
    /**
     *
     * 查找最新一条数据
     * @param {string} tableName 表名
     * @param {object} query 查询条件
     * @param {object} options 查询设置
     * @param {function} cb 回调函数
     * @memberof mongo
     */
    findOne(tableName, query, options, cb) {
        if(arguments.length === 3) {
            cb = options;
            options = {};
        }
        let sort = options.sort || {};
        this.connect().then(() => {
            schema[tableName].findOne(query, (err, docs)=> {
                cb(docs);
            }).sort(sort);
        });
    }
}
module.exports = mongo